package sicau.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import sicau.car.entity.Application;
import sicau.car.mapper.ApplicationMapper;
import sicau.car.service.ApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ITcz
 * @since 2020-07-28
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements ApplicationService {

    @Autowired
    ApplicationMapper applicationMapper;

    @Override
    public int order(Application applicationInform) {
        int insert = applicationMapper.insert(applicationInform);
        return insert;
    }

    @Override
    public IPage<Application> showPendingOrder(int page, int pageSize, int campus) {
        QueryWrapper<Application> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",2);
        queryWrapper.eq("campus", campus);
        queryWrapper.last("ORDER BY create_time DESC");
        Page<Application> page1 = new Page<>(page, pageSize);
        IPage<Application> applicationIPage = applicationMapper.selectPage(page1, queryWrapper);
        return applicationIPage;
    }

    @Override
    public void handleApplication(int applicationId) {

    }

    @Override
    public IPage<Application> showOrderHistory(int page, int pageSize, int campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("status",0,1);
        queryWrapper.eq("campus", campus);
        queryWrapper.last("ORDER BY create_time DESC");
        Page<Application> applicationPage = new Page<>(page, pageSize);
        Page page1 = applicationMapper.selectPage(applicationPage, queryWrapper);
        return page1;
    }

    @Override
    public Application selectByLicensePlate(String licensePlate, int campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("plate_id",licensePlate);
        queryWrapper.eq("campus", campus);
        Application application = applicationMapper.selectOne(queryWrapper);
        return application;
    }

    @Override
    public boolean addOrder(Application application) {
        int isInsert = applicationMapper.insert(application);
        if (isInsert == 0) {
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean agreeOrder(int applicationId){
        Application application = applicationMapper.selectById(applicationId);
        application.setStatus(1);
        int isSuccess = applicationMapper.updateById(application);
        if(isSuccess == 0) {
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean refuseOrder(Application application){
        int isSuccess = applicationMapper.updateById(application);
        if(isSuccess == 0) {
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Application selectById(Integer id) {
        return applicationMapper.selectById(id);
    }


}
